package com.way.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
@Slf4j
public class MessageSourceService {

    @Autowired
    private MessageSource messageSource;

    public String getMessage(String code, Object[] args, Locale locale) {
        return messageSource.getMessage(code,args,locale);
    }

    public String getMessage(String code) {
        try{
            return getMessage(code,null, LocaleContextHolder.getLocale());
        }catch (NoSuchMessageException e) {
            log.info("code is not exists:{}",code);
            return "";
        }
    }

    public String getLanguage() {
        return LocaleContextHolder.getLocale().getLanguage();
    }
}
